const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');

// 确保miniprogram_npm目录存在
const npmDistDir = path.join(__dirname, 'miniprogram_npm');
if (!fs.existsSync(npmDistDir)) {
  fs.mkdirSync(npmDistDir, { recursive: true });
}

// 复制TDesign组件库到miniprogram_npm目录
const tdesignSrc = path.join(__dirname, 'node_modules/tdesign-miniprogram/miniprogram_dist');
const tdesignDest = path.join(npmDistDir, 'tdesign-miniprogram');

console.log('正在构建npm包...');
console.log(`从 ${tdesignSrc} 复制到 ${tdesignDest}`);

// 递归复制目录
const copyDir = (src, dest) => {
  if (!fs.existsSync(dest)) {
    fs.mkdirSync(dest, { recursive: true });
  }
  
  const entries = fs.readdirSync(src, { withFileTypes: true });
  
  for (const entry of entries) {
    const srcPath = path.join(src, entry.name);
    const destPath = path.join(dest, entry.name);
    
    if (entry.isDirectory()) {
      copyDir(srcPath, destPath);
    } else {
      fs.copyFileSync(srcPath, destPath);
    }
  }
};

try {
  copyDir(tdesignSrc, tdesignDest);
  console.log('TDesign组件库复制成功！');
  console.log('npm构建完成！');
} catch (err) {
  console.error('构建失败:', err);
} 